package com.abl.manage.controller.sys;

import com.abl.core.common.util.MathKit;
import com.abl.core.domain.db.SysUser;
import com.abl.core.domain.project.MSG;
import com.abl.core.domain.project.SRS;
import com.abl.core.domain.project.ServiceException;
import com.abl.core.domain.project.TokenVO;
import com.abl.core.domain.project.PageSearch;
import com.abl.core.service.common.CaptchaService;
import com.abl.core.service.sys.SysUserService;
import com.alibaba.fastjson2.JSONObject;
import com.mybatisflex.core.paginate.Page;
import jakarta.annotation.Resource;
import org.redisson.api.RedissonClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/manage/system/user")
public class SysUserC {

    @Resource
    private RedissonClient redisson;

    @Resource
    private SysUserService sysUserService;

    @Resource
    private CaptchaService captchaService;

    @PostMapping("/insert")
    public SRS<Object> insert(SysUser param) {
        boolean add = sysUserService.insert(param);
        return SRS.bySuccess(add);
    }


    @PostMapping("/update")
    public SRS<Object> update(SysUser param) {
        boolean update = sysUserService.update(param);
        return SRS.bySuccess(update);
    }

    @PostMapping("/delete")
    public SRS<Object> delete(String ids) {
        sysUserService.delete(ids);
        return SRS.bySuccess();
    }

    @PostMapping("/page")
    public SRS<Page<SysUser>> page(SysUser sysUser, PageSearch pageSearch) {
        Page<SysUser> page = sysUserService.page(sysUser, pageSearch);

        return SRS.bySuccess(page);
    }

    @PostMapping("/userInfo")
    public SRS<Object> systemUserInfo() {
        SysUser sysUser = sysUserService.systemUserInfo();
        return SRS.bySuccess(sysUser);
    }

    @PostMapping("/login")
    public SRS<Object> systemUserLogin(String paramStr) throws Exception {
        JSONObject param = MathKit.rsaIsExpire(paramStr);
        if (param == null) throw new ServiceException(MSG.USER_LOGIN_PASSWORD_RE);

        captchaService.valid(param.getString("id"), param.getString("code"));

        TokenVO tokenVO = sysUserService.sysLogin(param.getString("account"), param.getString("password"), param.getString("type"));

        return SRS.bySuccess(tokenVO);
    }

    //刷新Token
    @PostMapping("/refreshToken")
    public SRS<TokenVO> refreshToken(String refreshToken) {
        TokenVO tokenVO = sysUserService.refreshToken(refreshToken);
        return SRS.bySuccess(tokenVO);
    }

    @PostMapping("/register")
    public SRS<Object> systemUserRegister(String paramStr) throws Exception {
        JSONObject param = MathKit.rsaIsExpire(paramStr);
        if (param == null) throw new ServiceException(MSG.USER_REGISTER_RE);

        captchaService.valid(param.getString("id"), param.getString("code"));

        boolean isSuccess = sysUserService.sysUserRegister(param.getString("account"), param.getString("password"), param.getString("type"));

        return SRS.bySuccess(isSuccess);
    }

    /**
     * 生成验证码
     */
    @PostMapping("/captchaImage")
    public SRS<Object> captchaImage() {
        return SRS.bySuccess(captchaService.createMathCaptchaImage());
    }


    @PostMapping("/resetUserPassword")
    public SRS<Boolean> resetUserPassword(String paramStr) throws Exception {
        JSONObject param = MathKit.rsaIsExpire(paramStr);
        if (param == null) throw new ServiceException(MSG.RSA_RE);
        return SRS.bySuccess(sysUserService.resetUserPassword(param.getString("userIds"), param.getString("newPassword")));
    }

    @PostMapping("/resetMyPassword")
    public SRS<Boolean> resetMyPassword(String paramStr) throws Exception {
        JSONObject param = JSONObject.parseObject(MathKit.rsaDecrypt(paramStr));
        if (param == null) throw new ServiceException(MSG.RSA_RE);
        return SRS.bySuccess(sysUserService.resetMyPassword(param.getString("oldPassword"), param.getString("newPassword")));
    }


}
